package com.yhy;

import com.alibaba.dubbo.common.logger.Logger;
import com.alibaba.dubbo.common.logger.LoggerFactory;
import com.alibaba.dubbo.common.utils.ConfigUtils;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import java.io.IOException;

/**
 * Created by yhy on 17-7-10.
 */
public class SpringContainer {
    private static final Logger logger = LoggerFactory.getLogger(com.alibaba.dubbo.container.spring.SpringContainer.class);
    public static final String SPRING_CONFIG = "dubbo.spring.config";
    public static final String DEFAULT_SPRING_CONFIG = "classpath*:META-INF/spring/*.xml";
    static ClassPathXmlApplicationContext context;

    public final static String wait = "";

    public SpringContainer() {
    }

    public static ClassPathXmlApplicationContext getContext() {
        return context;
    }

    public void start() {
        String configPath = ConfigUtils.getProperty("dubbo.spring.config");
        if (configPath == null || configPath.length() == 0) {
            configPath = "classpath*:META-INF/spring/*.xml";
        }

        context = new ClassPathXmlApplicationContext(configPath.split("[,\\s]+"));
        context.start();
    }

    public void stop() {
        try {
            if (context != null) {
                context.stop();
                context.close();
                context = null;
            }
        } catch (Throwable var2) {
            logger.error(var2.getMessage(), var2);
        }

    }


    public static void main( String args[] ) {
        final SpringContainer springContainer = new SpringContainer();
        springContainer.start();


        Runtime.getRuntime().addShutdownHook(new Thread(() -> {
                try {
                    springContainer.stop();
                } catch (Throwable var6) {
                    var6.printStackTrace();
                }
                synchronized(wait) {
                        wait.notify();
                    System.out.println("send sig ok");
                }

        }));


        synchronized(wait) {
            try {
                wait.wait();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }

        System.out.println("recive sig, exit ok");
    }
}